/*
 * Rust 整数运算详解 - 加减乘除与取余
 * 
 * 本程序详细介绍了Rust中整数的基本运算操作：
 * - 加法运算 (+): 两个数值相加
 * - 减法运算 (-): 两个数值相减  
 * - 乘法运算 (*): 两个数值相乘
 * - 除法运算 (/): 整数除法，结果向下取整
 * - 取余运算 (%): 求两个数相除的余数
 * 
 * 学习目标：
 * 1. 掌握五种基本算术运算符的使用方法
 * 2. 理解整数除法的特殊性（向下取整）
 * 3. 学习运算符的优先级和结合性
 * 4. 了解类型匹配和运算安全性
 * 5. 掌握复合赋值运算符的使用
 * 6. 学习实际编程中的运算应用场景
 */

fn main() {
    println!("🦀 欢迎学习 Rust 整数运算操作！");
    println!("{}", "=".repeat(50));
    
    // ===== 第一部分：基本算术运算符介绍 =====
    println!("\n📌 第一部分：五种基本算术运算符");
    
    println!("Rust 支持的整数算术运算符：");
    println!("➕ + (加法)：两个数值相加");
    println!("➖ - (减法)：两个数值相减");
    println!("✖️ * (乘法)：两个数值相乘");
    println!("➗ / (除法)：整数除法，结果向下取整");
    println!("📐 % (取余)：求两个数相除的余数");
    
    // ===== 第二部分：加法运算详解 =====
    println!("\n📌 第二部分：加法运算 (+)");
    
    let a = 15;
    let b = 25;
    let sum = a + b;
    
    println!("加法运算示例：");
    println!("  {} + {} = {}", a, b, sum);
    
    // 不同类型的加法
    let small1: i8 = 10;
    let small2: i8 = 20;
    let small_sum = small1 + small2;
    
    let large1: i64 = 1000000;
    let large2: i64 = 2000000;
    let large_sum = large1 + large2;
    
    println!("\n不同类型的加法：");
    println!("  i8:  {} + {} = {}", small1, small2, small_sum);
    println!("  i64: {} + {} = {}", large1, large2, large_sum);
    
    // 加法的实际应用
    println!("\n加法的实际应用场景：");
    let price1 = 299;  // 商品1价格
    let price2 = 599;  // 商品2价格
    let total_price = price1 + price2;
    println!("  购物车总价: {}元 + {}元 = {}元", price1, price2, total_price);
    
    let score1 = 85;   // 数学成绩
    let score2 = 92;   // 英语成绩
    let total_score = score1 + score2;
    println!("  总成绩: {} + {} = {}", score1, score2, total_score);
    
    // ===== 第三部分：减法运算详解 =====
    println!("\n📌 第三部分：减法运算 (-)");
    
    let minuend = 100;    // 被减数
    let subtrahend = 35;  // 减数
    let difference = minuend - subtrahend;
    
    println!("减法运算示例：");
    println!("  {} - {} = {}", minuend, subtrahend, difference);
    
    // 负数结果
    let result_negative = 20 - 50;
    println!("  负数结果: 20 - 50 = {}", result_negative);
    
    // 减法的实际应用
    println!("\n减法的实际应用场景：");
    let account_balance = 1000;  // 账户余额
    let withdrawal = 250;        // 提取金额
    let remaining_balance = account_balance - withdrawal;
    println!("  账户余额: {}元 - {}元 = {}元", account_balance, withdrawal, remaining_balance);
    
    let total_capacity = 500;    // 总容量
    let used_capacity = 320;     // 已用容量
    let free_capacity = total_capacity - used_capacity;
    println!("  剩余空间: {} - {} = {}", total_capacity, used_capacity, free_capacity);
    
    // ===== 第四部分：乘法运算详解 =====
    println!("\n📌 第四部分：乘法运算 (*)");
    
    let factor1 = 12;
    let factor2 = 8;
    let product = factor1 * factor2;
    
    println!("乘法运算示例：");
    println!("  {} × {} = {}", factor1, factor2, product);
    
    // 大数乘法
    let big_num1: i64 = 123456;
    let big_num2: i64 = 789;
    let big_product = big_num1 * big_num2;
    println!("  大数乘法: {} × {} = {}", big_num1, big_num2, big_product);
    
    // 乘法的实际应用
    println!("\n乘法的实际应用场景：");
    let unit_price = 25;         // 单价
    let quantity = 12;           // 数量
    let total_cost = unit_price * quantity;
    println!("  总价计算: {}元/个 × {}个 = {}元", unit_price, quantity, total_cost);
    
    let width = 15;              // 宽度
    let height = 20;             // 高度
    let area = width * height;
    println!("  面积计算: {} × {} = {}", width, height, area);
    
    let days_per_week = 7;
    let weeks = 4;
    let total_days = days_per_week * weeks;
    println!("  时间计算: {}天/周 × {}周 = {}天", days_per_week, weeks, total_days);
    
    // ===== 第五部分：除法运算详解 =====
    println!("\n📌 第五部分：除法运算 (/)");
    
    let dividend = 42;    // 被除数
    let divisor = 7;      // 除数
    let quotient = dividend / divisor;
    
    println!("除法运算示例：");
    println!("  {} ÷ {} = {}", dividend, divisor, quotient);
    
    // 整数除法的特性：向下取整
    println!("\n整数除法的重要特性（向下取整）：");
    let examples = [(10, 3), (17, 4), (25, 6), (100, 7)];
    
    for (a, b) in examples {
        let result = a / b;
        let remainder = a % b;
        println!("  {} ÷ {} = {} 余 {} (完整：{}.{}...)", a, b, result, remainder, result, remainder);
    }
    
    // 除法的实际应用
    println!("\n除法的实际应用场景：");
    let total_items = 100;       // 总物品数
    let items_per_box = 12;      // 每箱物品数
    let boxes_needed = total_items / items_per_box;
    let leftover_items = total_items % items_per_box;
    println!("  包装计算: {}个物品 ÷ {}个/箱 = {}箱，剩余{}个", 
             total_items, items_per_box, boxes_needed, leftover_items);
    
    let total_seconds = 3665;    // 总秒数
    let hours = total_seconds / 3600;
    let minutes = (total_seconds % 3600) / 60;
    let seconds = total_seconds % 60;
    println!("  时间转换: {}秒 = {}小时{}分钟{}秒", total_seconds, hours, minutes, seconds);
    
    // ===== 第六部分：取余运算详解 =====
    println!("\n📌 第六部分：取余运算 (%)");
    
    let num1 = 17;
    let num2 = 5;
    let remainder = num1 % num2;
    
    println!("取余运算示例：");
    println!("  {} % {} = {} ({}除以{}余{})", num1, num2, remainder, num1, num2, remainder);
    
    // 取余运算的规律
    println!("\n取余运算的数学规律：");
    let test_cases = [10, 11, 12, 13, 14, 15, 16];
    let modulus = 4;
    
    print!("  对4取余的循环模式: ");
    for num in test_cases {
        print!("{}%{}={} ", num, modulus, num % modulus);
    }
    println!();
    
    // 取余的实际应用
    println!("\n取余运算的实际应用场景：");
    
    // 判断奇偶性
    let number = 15;
    if number % 2 == 0 {
        println!("  {} 是偶数", number);
    } else {
        println!("  {} 是奇数", number);
    }
    
    // 循环索引计算
    let array_size = 5;
    let indices = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
    print!("  循环数组索引: ");
    for i in indices {
        let cyclic_index = i % array_size;
        print!("{}→{} ", i, cyclic_index);
    }
    println!();
    
    // 分组计算
    let total_students = 25;
    let group_size = 6;
    let full_groups = total_students / group_size;
    let remaining_students = total_students % group_size;
    println!("  分组计算: {}名学生，{}人/组 = {}个完整组 + {}人", 
             total_students, group_size, full_groups, remaining_students);
    
    // ===== 第七部分：运算符优先级 =====
    println!("\n📌 第七部分：运算符优先级和结合性");
    
    println!("运算符优先级（从高到低）：");
    println!("  1. () 括号 - 最高优先级");
    println!("  2. *, /, % 乘法、除法、取余");
    println!("  3. +, - 加法、减法");
    
    let expr1 = 2 + 3 * 4;           // 2 + (3 * 4) = 14
    let expr2 = (2 + 3) * 4;         // (2 + 3) * 4 = 20
    let expr3 = 10 - 6 / 2;          // 10 - (6 / 2) = 7
    let expr4 = (10 - 6) / 2;        // (10 - 6) / 2 = 2
    
    println!("\n优先级示例：");
    println!("  2 + 3 * 4 = {} (先乘后加)", expr1);
    println!("  (2 + 3) * 4 = {} (括号优先)", expr2);
    println!("  10 - 6 / 2 = {} (先除后减)", expr3);
    println!("  (10 - 6) / 2 = {} (括号优先)", expr4);
    
    // 复杂表达式
    let complex_expr = 20 + 15 * 2 / 3 - 4 % 3;
    println!("  复杂表达式: 20 + 15 * 2 / 3 - 4 % 3 = {}", complex_expr);
    println!("  计算过程: 20 + (15 * 2) / 3 - (4 % 3) = 20 + 30/3 - 1 = 20 + 10 - 1 = 29");
    
    // ===== 第八部分：复合赋值运算符 =====
    println!("\n📌 第八部分：复合赋值运算符");
    
    println!("Rust 支持的复合赋值运算符：");
    println!("  += (加法赋值), -= (减法赋值), *= (乘法赋值)");
    println!("  /= (除法赋值), %= (取余赋值)");
    
    let mut value = 100;
    println!("\n复合赋值运算符示例（初始值: {}）：", value);
    
    value += 50;  // 等价于 value = value + 50;
    println!("  value += 50  → value = {}", value);
    
    value -= 30;  // 等价于 value = value - 30;
    println!("  value -= 30  → value = {}", value);
    
    value *= 2;   // 等价于 value = value * 2;
    println!("  value *= 2   → value = {}", value);
    
    value /= 4;   // 等价于 value = value / 4;
    println!("  value /= 4   → value = {}", value);
    
    value %= 10;  // 等价于 value = value % 10;
    println!("  value %= 10  → value = {}", value);
    
    // ===== 第九部分：类型安全和运算注意事项 =====
    println!("\n📌 第九部分：类型安全和运算注意事项");
    
    println!("Rust 整数运算的重要特性：");
    println!("  ✅ 类型安全：不同类型不能直接运算");
    println!("  ✅ 溢出检查：debug模式下会检查溢出");
    println!("  ✅ 除零检查：除零会导致 panic");
    
    // 同类型运算
    let int1: i32 = 100;
    let int2: i32 = 25;
    let result_same_type = int1 + int2;
    println!("\n同类型运算: i32({}) + i32({}) = {}", int1, int2, result_same_type);
    
    // 类型转换后运算
    let small_num: i8 = 10;
    let large_num: i32 = 100;
    let result_converted = large_num + (small_num as i32);
    println!("类型转换运算: i32({}) + i8({} as i32) = {}", large_num, small_num, result_converted);
    
    // ===== 第十部分：实际编程应用场景 =====
    println!("\n📌 第十部分：实际编程中的运算应用");
    
    // 计算平均值
    println!("\n1. 平均值计算：");
    let scores = [85, 92, 78, 96, 88];
    let sum_scores: i32 = scores[0] + scores[1] + scores[2] + scores[3] + scores[4];
    let average = sum_scores / scores.len() as i32;
    println!("   成绩: {:?}", scores);
    println!("   平均分: {} ÷ {} = {}", sum_scores, scores.len(), average);
    
    // 温度转换
    println!("\n2. 温度转换（华氏度转摄氏度）：");
    let fahrenheit = 100;
    let celsius = (fahrenheit - 32) * 5 / 9;  // 使用整数运算
    println!("   {}°F = {}°C（整数计算）", fahrenheit, celsius);
    
    // 计算复利（简化版）
    println!("\n3. 简单利息计算：");
    let principal = 10000;  // 本金
    let rate = 5;           // 利率（百分比）
    let years = 3;          // 年数
    let simple_interest = principal * rate * years / 100;
    let total_amount = principal + simple_interest;
    println!("   本金: {}元，利率: {}%，年数: {}年", principal, rate, years);
    println!("   利息: {}元，总额: {}元", simple_interest, total_amount);
    
    // 数字分解
    println!("\n4. 数字各位分解：");
    let number = 12345;
    let units = number % 10;
    let tens = (number / 10) % 10;
    let hundreds = (number / 100) % 10;
    let thousands = (number / 1000) % 10;
    let ten_thousands = number / 10000;
    
    println!("   数字 {} 的各位分解：", number);
    println!("   万位: {}, 千位: {}, 百位: {}, 十位: {}, 个位: {}", 
             ten_thousands, thousands, hundreds, tens, units);
    
    // ===== 总结 =====
    println!("\n🎯 学习总结：");
    println!("1. Rust 支持五种基本算术运算符：+, -, *, /, %");
    println!("2. 整数除法会向下取整，取余运算求得除法余数");
    println!("3. 运算符有明确的优先级，括号可以改变优先级");
    println!("4. 复合赋值运算符提供了简洁的写法");
    println!("5. Rust 保证类型安全，不同类型需要显式转换");
    println!("6. 整数运算在实际编程中应用广泛");
    
    println!("\n🚀 恭喜！你已经掌握了 Rust 整数运算的核心知识！");
}
